package com.blb.community_admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blb.community_admin.Annotation.Log;
import com.blb.community_admin.entity.SysPost;
import com.blb.community_admin.entity.SysUserPost;
import com.blb.community_admin.service.ISysPostService;
import com.blb.community_admin.service.ISysUserPostService;
import com.blb.community_admin.util.EasyExcelUtil;
import com.blb.community_admin.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hkp
 */
@Api("岗位管理")
@RestController
public class SysPostController {

    @Resource
    private ISysPostService sysPostService;

    @Resource
    private ISysUserPostService sysUserPostService;

    @ApiOperation("岗位查询")
    @GetMapping("/post/list")
    public ResponseResult<List<SysPost>> getPost(HttpServletRequest request) {
        List<SysPost> list = sysPostService.list(null);
        return ResponseResult.ok(list);
    }

    /**
     * 根据编码，名称，状态进行模糊分页查询
     *
     * @param current
     * @param size
     * @param postCode
     * @param postName
     * @param status
     * @return
     */
    @ApiOperation("分页模糊查询")
    @GetMapping("/post/page/fuzzy")
    public ResponseResult<IPage<SysPost>> selectPostPage(Integer current, Integer size, String postCode, String postName, String status) {
        IPage<SysPost> page = sysPostService.selectPostPage(current, size, postCode, postName, status);
        return ResponseResult.ok(page);
    }

    /**
     * 添加
     *
     * @param sysPost
     * @return
     */
    @Log(module = "岗位管理", operationType = 1, requestMethod = "POST")
    @ApiOperation("添加岗位")
    @PostMapping("/post")
    public ResponseResult<String> add(@RequestBody SysPost sysPost) {
        sysPost.setCreateTime(LocalDateTime.now());
        sysPostService.save(sysPost);
        return ResponseResult.ok("OK");
    }

    /**
     * 修改岗位
     *
     * @param sysPost
     * @return
     */
    @Log(module = "岗位管理", operationType = 2, requestMethod = "PUT")
    @ApiOperation("修改岗位")
    @PutMapping("/post")
    public ResponseResult<String> update(@RequestBody SysPost sysPost) {
        sysPost.setUpdateTime(LocalDateTime.now());
//        拿到原来的post状态
        SysPost post = sysPostService.getById(sysPost.getPostId());
//        如果没改状态直接修改返回OK
        if (post.getStatus().equals(sysPost.getStatus())) {
            sysPostService.updateById(sysPost);
            return ResponseResult.ok("OK");
//            如果改了，看有没有该岗位的用户，有不能改，没有可以改
        } else {
            List<SysUserPost> list = sysUserPostService.list(new QueryWrapper<SysUserPost>().lambda().eq(SysUserPost::getPostId, sysPost.getPostId()));
            if (list.size() == 0) {
                sysPostService.updateById(sysPost);
                return ResponseResult.ok("OK");
            } else {
                return ResponseResult.ok("该岗位上有员工，不能停用");
            }
        }
    }

    @Log(module = "岗位管理", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("根据id删除")
    @DeleteMapping("/post/{postId}")
    public ResponseResult<String> deleteById(@PathVariable Long postId) {
        List<SysUserPost> list = sysUserPostService.list(new QueryWrapper<SysUserPost>().lambda().eq(SysUserPost::getPostId, postId));
        if (list.size() == 0) {
            sysPostService.removeById(postId);
            return ResponseResult.ok("OK");
        } else {
            return ResponseResult.ok("该岗位上有员工，不能删除");
        }
    }

    /**
     * 导出
     *
     * @param sysPosts
     * @return
     * @throws IOException
     */
    @Log(module = "岗位管理", operationType = 5, requestMethod = "POST")
    @ApiOperation("导出")
    @PostMapping("/post/export")
    public ResponseEntity<ByteArrayResource> export(@RequestBody List<SysPost> sysPosts) throws IOException {
        String path = EasyExcelUtil.write("岗位管理", SysPost.class, sysPosts);
        ResponseEntity<ByteArrayResource> download = EasyExcelUtil.download(path);
        return download;
    }

    @Log(module = "岗位管理", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("根据岗位编号批量删除")
    @DeleteMapping("/post/delete")
    public ResponseResult<String> deleteByIds(@RequestBody List<SysPost> selections) {
        int num = (int) selections.stream().map(sysPost -> sysUserPostService.list(new QueryWrapper<SysUserPost>().lambda().eq(SysUserPost::getPostId, sysPost.getPostId()))).filter(list -> list.size() != 0).count();
        if (num != 0) {
            return ResponseResult.ok("该岗位上有员工，不能删除");
        }
        ArrayList<Long> list = new ArrayList<>();
        selections.stream().forEach(sysPost -> {
            list.add(sysPost.getPostId());
        });
        sysPostService.removeByIds(list);
        return ResponseResult.ok("OK");
    }
}
